package com.cn.dream_order_server.service.impl;

import com.cn.dream_order_server.client.GoodServer;
import com.cn.dream_order_server.mapper.OrderShoppingMapper;
import com.cn.dream_order_server.pojo.GoodCommoditySku;
import com.cn.dream_order_server.pojo.OrderShopping;
import com.cn.dream_order_server.pojo.OrderShoppingExample;
import com.cn.dream_order_server.service.OrderCartService;
import com.cn.dream_order_server.util.JsonUtils;
import com.cn.dream_order_server.vo.Cartvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderCartServiceImpl implements OrderCartService {



    @Autowired
    private OrderShoppingMapper orderShoppingMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private GoodServer goodServer;

    @Override
    public Map<String,Object> getOrederShoppingListByUserId(String userId) {





        //
        OrderShoppingExample example = new OrderShoppingExample();
        OrderShoppingExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        Long total = 0L;
        List<OrderShopping> orderShoppingList = orderShoppingMapper.selectByExample(example);
        for (OrderShopping orderShopping:orderShoppingList) {
           /* GoodCommoditySku goodCommoditySku = map1.get(orderShopping.getCommodityId());*/
            //调用good服务获取对象
            String cartVo = goodServer.getCartVo(orderShopping.getCommodityId());
            Cartvo cartvo = JsonUtils.stringToObj(cartVo, Cartvo.class);
            String[] split = cartvo.getSku().getImages().split(",");
            cartvo.getSku().setImages(split[0]);
            orderShopping.setGoodCommoditySku(cartvo.getSku());
            total=total+orderShopping.getShoppingTotal();
            orderShopping.setShoppingTotal(orderShopping.getShoppingTotal());
        }
        Long cartTotal =  total;
        Map map = new HashMap();
        map.put("cartTotal",cartTotal);
        map.put("orderShoppingList",orderShoppingList);
        return map;
    }


    @Override
    public void updateOrderShoppingByShoppingId(String shoppingId , Integer num) {

        String s2 = stringRedisTemplate.opsForValue().get("orderCart-all");

        String s = stringRedisTemplate.opsForValue().get("orderCart-" + shoppingId);
        OrderShopping orderShopping = null;
        if (s != null && !"".equals(s)){
            orderShopping = JsonUtils.stringToObj(s, OrderShopping.class);
        }else{
            orderShopping = orderShoppingMapper.selectByPrimaryKey(shoppingId);
            //调用good服务获取对象
            String cartVo = goodServer.getCartVo(orderShopping.getCommodityId());
            Cartvo cartvo = JsonUtils.stringToObj(cartVo, Cartvo.class);
            orderShopping.setGoodCommoditySku(cartvo.getSku());
            if(s2!=null&&!"".equals(s2)){
                stringRedisTemplate.opsForValue().set("orderCart-all",s2+",orderCart-"+ shoppingId);
            }else{
                stringRedisTemplate.opsForValue().set("orderCart-all","orderCart-"+ shoppingId);
            }
        }
        orderShopping.setShoppingTotal(orderShopping.getGoodCommoditySku().getPrice()*num);
        orderShopping.setShoppingNum(num);
        String s1 = JsonUtils.objToString(orderShopping);
        stringRedisTemplate.opsForValue().set("orderCart-"+ shoppingId,s1);
    }

    @Override
    public void updateOrderCartTable() {
        String s = stringRedisTemplate.opsForValue().get("orderCart-all");
        if(s!=null && !"".equals(s)){
            String[] strs = s.split(",");
            for (int i = 0; i < strs.length; i++) {
                String s1 = stringRedisTemplate.opsForValue().get(strs[i]);
                if(s1 != null && !"".equals(s1)){
                    OrderShopping orderShopping = JsonUtils.stringToObj(s1, OrderShopping.class);
                    orderShoppingMapper.updateByPrimaryKeySelective(orderShopping);
                }
                stringRedisTemplate.delete(strs[i]);
            }
            stringRedisTemplate.delete("orderCart-all");
        }
    }

    @Override
    public Integer deleteOrderShoppingByShoppingId(String shoppingId) {
        OrderShopping orderShopping = orderShoppingMapper.selectByPrimaryKey(shoppingId);
        if(orderShopping!=null){
            return orderShoppingMapper.deleteByPrimaryKey(shoppingId);
        }

        return null;
    }

    @Override
    public Integer saveOrderShoppingByCommodity_id(String commodity_id,Integer shopping_num,Long price,String userId) {
        //创建查询条件
        OrderShoppingExample example = new OrderShoppingExample();
        OrderShoppingExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andCommodityIdEqualTo(commodity_id);
        //使用mapper查询数据
        List<OrderShopping> orderShoppingList = orderShoppingMapper.selectByExample(example);
        //判断查询的结果是否存在
        if(orderShoppingList!=null && orderShoppingList.size()>0){
            return null;
        }
        //不存在就添加到数据库
        OrderShopping orderShopping = new OrderShopping();
        orderShopping.setShoppingId(UUID.randomUUID().toString());
        orderShopping.setCommodityId(commodity_id);
        orderShopping.setShoppingNum(shopping_num);
        orderShopping.setUserId(userId);
        orderShopping.setShoppingTotal(price*shopping_num);
        //使用mapper添加数据数据
        return orderShoppingMapper.insertSelective(orderShopping);

    }

    @Override
    public Integer updateOrderCartByShoppingId(OrderShopping orderShopping) {
        OrderShopping shopping = orderShoppingMapper.selectByPrimaryKey(orderShopping.getShoppingId());
        if (shopping!=null){
            return orderShoppingMapper.updateByPrimaryKeySelective(orderShopping);
        }
        return null;
    }

    @Override
    public List<OrderShopping> selectOrderShoppingListInOrderShoppingId(String[] shoppingIds) {
        //创建查询条件(id in 集合)
        OrderShoppingExample example = new OrderShoppingExample();
        OrderShoppingExample.Criteria criteria = example.createCriteria();
        //将传入的数组转换成list集合
        criteria.andShoppingIdIn(Arrays.asList(shoppingIds));
        List<OrderShopping> list = orderShoppingMapper.selectByExample(example);
        return list;
    }
}
